Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior 您所在的位置:网站首页 be intended to Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior

Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior

#Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior| 来源: 网络整理| 查看: 265

错误提示

Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior

[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ //此处下划线方式调用成员变量时报此错误 // Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior _label.frame = CGRectMake(0, 0, 100, 50); } completion:^(BOOL finished) { }]; 提示原因

Xcode9.3 后在 block 里面需要使用 self 调用成员属性,通过下划线方式调用就会发出此警告,用于提示开发者此处有可能会产生循环引用,而通过 self 显示调用属性更容易排查问题。

解决方案

方案一:

既然提示是为了方便排查循环引用问题,那么可以通过解决循环引用问题来消除提示,即使用weakSelf:

__weak typeof(self) weakSelf=self; //然后将警告的短横线用weakself代替。 [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ __strong typeof(weakSelf) strongSelf = weakSelf; strongSelf.label.frame = CGRectMake(0, 0, 100, 50); } completion:^(BOOL finished) { }];

方案二:

此方案只是可以消除提示 ,并不能真正解决循环引用问题 ,不建议使用:

截屏2022-07-15 下午1.57.53.png 总结

想要防止block循环引用,消除警告,就使用__weak 。

文章持续更新中、希望对各位有所帮助、有问题可留言 大家共同学习.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有